package com.atguigu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ServletContext类的作用
 *
 * @author Mc.Chai
 * @program: JavaWeb
 * @create 2021-12-05-14:54 14:54
 */

public class ServletContext extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("---ServletContext--start-");
//      1.获取web.xml中配置的上下文参数context-param  使用ServletContext.getInitParameter(),并不能此去获取<init-param>中的值
        javax.servlet.ServletContext context = getServletConfig().getServletContext(); //获取ServletContext对象
        System.out.println("<context-param>  name=namespace的值 " + context.getInitParameter("namespace"));

//        2.获取当前的工作路径，格式：/工程路径  getContextPath()
        System.out.println("当前工程路径 = " + context.getContextPath());//打印：当前工程路径 = /01_servlet
//        3.获取工程部署后在服务器硬盘上的绝对路径 getRealPath("/")
        /**
         *   /  斜杠被服务器解析地址为：http://ip:port/工程名/ 映射到IDEA代码的web目录
         * */
        System.out.println("工程部署的路径是 = " + context.getRealPath("/"));//到达web目录下
        //打印：工程部署的路径是 = D:\代码文件夹\Idea Projects\JavaWeb\out\artifacts\01_servlet_war_exploded\
//        4.像Map一样存取数据

    }
}
